//
//  TabBarViewController.m
//  ijovo
//
//  Created by 吴柳燕 on 15/7/16.
//  Copyright (c) 2015年 wly. All rights reserved.
//

#import "TabBarViewController.h"
#import "AppDelegate.h"
#import "UITabBar+badge.h"
#import "chartsVC.h"
#import "MyVC.h"
#import "HomeVC.h"
#import "Home_PlanVC.h"
#import "MyVM.h"
#import "HomePageVM.h"

@interface TabBarViewController ()


@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *userId =[UserModel userModel].UserId;
    if (userId) {
        [[MyVM manage] getUserInfoWithUserId:userId WithSuccess:^(id returnValue) {
            [UserModel setAvater:[UserInfoModel share].Avater];
        } andFail:^(NSString *errorMessage, NSError *error) {
            
        }];
    }
    
    ///获取初始化数据
    NSMutableArray *vcArr = [NSMutableArray array];
    //首页
    UINavigationController *homeNC = [[UINavigationController alloc] initWithRootViewController:[[HomeVC alloc]init]];
    homeNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"bar_home"] tag:0];
    homeNC.tabBarItem.selectedImage=[UIImage imageNamed:@"bar_home_s"];
    [vcArr addObject:homeNC];
//    //走势图
//    UINavigationController *chartsNC = [[UINavigationController alloc] initWithRootViewController:[[ChartsVC alloc]init]];
//    chartsNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"走势" image:[UIImage imageNamed:@"bar_movements"] tag:1];
//    chartsNC.tabBarItem.selectedImage=[UIImage imageNamed:@"bar_movements_s"];
//    [vcArr addObject:chartsNC];
    //聊天
//    UINavigationController *chatNC = [[UINavigationController alloc] initWithRootViewController:[[ChatVC alloc]init]];
//    chatNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"聊天" image:[UIImage imageNamed:@"bar_chat"] tag:1];
//    chatNC.tabBarItem.selectedImage=[UIImage imageNamed:@"bar_chat_s"];
//    [vcArr addObject:chatNC];
    //计划
    UINavigationController *foundNC = [[UINavigationController alloc] initWithRootViewController:[[Home_PlanVC alloc]init]];
    foundNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"计划" image:[UIImage imageNamed:@"bar_plan"] tag:2];
    foundNC.tabBarItem.selectedImage=[UIImage imageNamed:@"bar_plan_s"];
    [vcArr addObject:foundNC];
    //我的
    UINavigationController *myNC = [[UINavigationController alloc] initWithRootViewController:[[MyVC alloc]init]];
    myNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"bar_my"] tag:3];
    myNC.tabBarItem.selectedImage=[UIImage imageNamed:@"bar_my_s"];
    [vcArr addObject:myNC];
    self.viewControllers = vcArr;
    self.tabBar.tintColor =NAVCOLOR;
    WS(weakSelf);
    [[HomePageVM manage] getVersionWithId:UpdateKey andSuccess:^(id returnValue) {
        NSString *strUrl=[returnValue objectForKey:@"Downurl"];
        if (strUrl.length>0) {
            [weakSelf checkVersion:returnValue];
        }
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)checkVersion:(NSDictionary *)dict{
    switch ([[dict objectForKey:@"Status"] integerValue]) {
        case 2:
            [self showAlert:2 andContent:[dict objectForKey:@"Content"] andUrl:[dict objectForKey:@"Downurl"]];
            break;
        case 3:
            [self showAlert:3 andContent:[dict objectForKey:@"Content"] andUrl:[dict objectForKey:@"Downurl"]];
            break;
        default:
            break;
    }
}

- (void)showAlert:(NSInteger )status andContent:(NSString *)content andUrl:(NSString *)url{
    NSString *Title = @"更新提示";
    content=[content stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];
    content=[content stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:Title message:content preferredStyle:UIAlertControllerStyleAlert];
    if (status==2) {
        UIAlertAction *cancleAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: url]];
            exit(0);
        }];
        [alertController addAction:sureAction];
        [alertController addAction:cancleAction];
    }else if (status==3){
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: url]];
            exit(0);
        }];
        [alertController addAction:sureAction];
    }
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
